unit clsVolumes;

interface

uses Dm, dbtables, grids, clsproduto;

type
  TVolumes = class(TObject)
  private
    function RecuperarporEAN(pro_codigo, ean: string): boolean;
  public
    Pro_Codigo: string;
    Vol_Codigo: string;
    Vol_EAN: string;
    function RecuperarProduto(vol_codigo: string): boolean;
    function RecuperarVolumes(pro_codigo: string; var tq: TQuery): boolean;
    function Recuperar(pro_codigo, vol_codigo: string): boolean;
    function RecuperarVolume_porEAN(pro_codigo, ean: string): string;
    procedure Gravar(pro_codigo, vol_codigo: string);
    procedure Excluir(pro_codigo, vol_codigo: string);
    procedure ExcluirTudo(pro_codigo: string);
    procedure Consultar(var p_q: TQuery; pro_codigo: string);
    procedure ConsultarVolumes(var p_q: TQuery; vol_codigo: string);
    procedure Atualiza_EAN(p_Prod, p_Vol, p_EAN: string);
    procedure Elimina_Volume_EAN(p_Prod, p_EAN: string);
  end;

implementation

uses DB;

{ TVolumes }

procedure TVolumes.Atualiza_EAN(p_Prod, p_Vol, p_EAN: string);
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Volumes set Vol_EAN =:EAN where Pro_Codigo =:a and Vol_Codigo =:b');
  tq.Params[0].AsString := p_EAN;
  tq.Params[1].asstring := p_Prod;
  tq.params[2].asstring := p_Vol;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TVolumes.Consultar(var p_q: TQuery; pro_codigo: string);
begin
  p_q.Close;
  p_q.Params.Clear;
  p_q.SQL.Clear;
  p_q.SQL.Add('Select distinct v.* , p.Pro_nome from Volumes v, produto P ' +
    'where (v.Vol_Codigo = p.Pro_Codigo) and v.pro_codigo=:a');
  p_q.Params[0].AsString := pro_codigo;
  p_q.Open;
end;

procedure TVolumes.ConsultarVolumes(var p_q: TQuery; vol_codigo: string);
begin
  p_q.Close;
  p_q.Params.Clear;
  p_q.SQL.Clear;
  p_q.SQL.Add('Select distinct pro_codigo from Volumes where vol_codigo=:a');
  p_q.Params[0].AsString := vol_codigo;
  p_q.Open;
end;

procedure TVolumes.Elimina_Volume_EAN(p_Prod, p_EAN: string);
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('delete from ProdEAN where Pro_Codigo =:a and Pro_EAN =:b');
  tq.Params[0].AsString := p_Prod;
  tq.Params[1].asstring := p_EAN;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TVolumes.Excluir(pro_codigo, vol_codigo: string);
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Delete from Volumes where Pro_Codigo =:a and Vol_Codigo =:b');
  tq.Params[0].asstring := pro_codigo;
  tq.params[1].asstring := vol_codigo;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TVolumes.ExcluirTudo(pro_codigo: string);
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Delete from Volumes where Pro_Codigo =:a ');
  tq.Params[0].asstring := pro_codigo;
  tq.execsql;
  tq.close;
  tq.Free;

end;

procedure TVolumes.Gravar(pro_codigo, vol_codigo: string);
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into volumes values (:a,:b,:c)');
  tq.Params[0].asstring := pro_codigo;
  tq.params[1].asstring := vol_codigo;
  tq.Params[2].AsString := Vol_EAN;
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TVolumes.Recuperar(pro_codigo, vol_codigo: string): boolean;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Volumes where pro_codigo =:a and vol_codigo =b:');
  tq.Params[0].AsString := pro_codigo;
  tq.params[1].AsString := vol_codigo;
  tq.Open;
  if tq.Eof then
    Result := False
  else
    Result := True;
  Pro_Codigo := pro_codigo;
  Vol_Codigo := vol_codigo;
  Vol_EAN := tq.FieldByName('Vol_EAN').AsString;
  tq.Close;
  tq.Free;
end;

function TVolumes.RecuperarporEAN(pro_codigo, ean: string): boolean;
begin

end;

function TVolumes.RecuperarProduto(vol_codigo: string): Boolean;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select Pro_Codigo from Volumes where vol_codigo =:a');
  tq.Params[0].AsString := vol_codigo;
  tq.Open;
  if tq.Eof then
    Result := False
  else
    Result := True;
  Pro_Codigo := tq.FieldByName('Pro_Codigo').AsString;
  tq.Close;
  tq.Free;
end;

function TVolumes.RecuperarVolumes(pro_codigo: string; var tq: TQuery): boolean;
begin
  //tq             := TQuery.Create(DmInt);
  //tq.databasename:= dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Volumes where pro_codigo =:a');
  tq.Params[0].AsString := pro_codigo;
  tq.Open;
  if tq.Eof then
    Result := False
  else
    Result := True;
  //Vol_Codigo:= tq.FieldByName('Vol_Codigo').AsString;
  //tq.Close;
  //tq.Free;
end;

function TVolumes.RecuperarVolume_porEAN(pro_codigo, ean: string): string;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Volumes where pro_codigo =:a and vol_ean =:b');
  tq.Params[0].AsString := pro_codigo;
  tq.params[1].AsString := ean;
  tq.Open;
  if tq.Eof then
    Result := ''
  else
    Result := tq.FieldByName('Vol_Codigo').asstring;
  tq.Close;
  tq.Free;
end;

end.
